package com.sisyphus.mock.utils;

import android.app.Application;
import android.text.TextUtils;
import android.webkit.MimeTypeMap;

import com.sisyphus.mock.MockApplicationServer;
import com.yanzhenjie.andserver.http.multipart.MultipartFile;
import com.yanzhenjie.andserver.util.IOUtils;

import java.io.File;
import java.util.UUID;

public class FileUtil {

    public static File createRandomFile(MultipartFile file) {
        String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(file.getContentType().toString());
        if (TextUtils.isEmpty(extension)) {
            extension = MimeTypeMap.getFileExtensionFromUrl(file.getFilename());
        }

        String uuid = UUID.randomUUID().toString();

        Application context = MockApplicationServer.sApplication;

        File mockImageFile = new File(context.getFilesDir(), "_mock_image_file");
        IOUtils.createFolder(mockImageFile);

        return new File(mockImageFile, uuid + "." + extension);
    }

}
